package com.hxd.mall.utils.open

import android.app.Activity
import android.content.Context
import android.view.View
import android.view.ViewGroup
import android.view.inputmethod.InputMethodManager
import com.hxd.mall.base.IBaseView

/**
 * 键盘相关处理
 */
object KeyboardUtil {
    /**
     * 收起键盘
     */
    fun hideKeyBoard(view: IBaseView) {
        ((view as Activity).getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager).hideSoftInputFromWindow((view as Activity).window.decorView.windowToken, 0)
    }
    /**
     * 收起键盘
     */
    fun hideKeyBoardActivity(activity: Activity) {
        (activity.getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager).hideSoftInputFromWindow(activity.window.decorView.windowToken, 0)
    }

    /**
     * 键盘高度监听
     */
    fun getKeyBoardStatusChangeListener(softBottomHeight: Int, view: View): KeyboardHelperUtil.OnKeyboardStatusChangeListener {
        return object : KeyboardHelperUtil.OnKeyboardStatusChangeListener {
            override fun onKeyboardPop(keyboardHeight: Int) {
                if (softBottomHeight >= keyboardHeight)
                    return
                val lpNew = view.layoutParams as ViewGroup.MarginLayoutParams
                lpNew.topMargin = softBottomHeight - keyboardHeight
                view.layoutParams = lpNew
            }

            override fun onKeyboardClose(keyboardHeight: Int) {
                if (softBottomHeight >= keyboardHeight)
                    return
                val lpNew = view.layoutParams as ViewGroup.MarginLayoutParams
                lpNew.topMargin = 0
                view.layoutParams = lpNew
            }
        }
    }
}